<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body style="text-align: center;">
    <textarea name="" id="result" style="width: 50%;height: 400px;"></textarea>
    <div>
        <button onclick="printMyArray()">打印数组</button>
        <button onclick="loopMyArray()">循环数组</button>
        <button onclick="filterMyArray()">过滤数组</button>
        <button onclick="sliceMyArray()">截取数组</button>
        <button onclick="spliceMyArray()">数组元素删除</button>
        <button onclick="joinMyArray()">数组元素分隔</button>
        <button onclick="concatMyArray()">数组转换</button>
        <button onclick="sortMyArray()">数组排序</button>
    </div>
    <script>
        let a=[0,1,2,3,4,5,6,7,8,9]
        function printMyArray(){
            document.querySelector("#result").value=`[${a}]`;
        }
        function loopMyArray(){
            a.forEach(function(item,index){
                console.log(item,index)
            });
        }
        function filterMyArray(){
            // 将大于5的元素过滤出来
            let res=a.filter(function(item,index){
            if(item<=5) return true;
            return false
            });
            console.log(res)
            printMyArray(res)
        }
        function sliceMyArray(){
            // 截取数组里面的第二个到第六个元素,生成新的数组
            let res=a.slice(1,6) //[1,6)
            console.log(res)
            printMyArray(res)
        }
        function spliceMyArray(){
            // 删除数组的第六个元素
            let res=a.splice(5)
            console.log(res)
            printMyArray(res)
        }
        function joinMyArray(){
            let res=a.join('-')
            console.log(res)
        }
        function concatMyArray(){
            let b=[...a]
            b[0]=100
            console.log(a,b)
        }
        function sortMyArray(){
            let res=a.sort(function(right,left){
                if(left<right)return 1;
                return -1
            })
            console.log(a,res)
            printMyArray(res)
        }
    </script>
</body>
</html>